package com.tangtanwei.hotel.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangtanwei.hotel.entity.Order;
import com.tangtanwei.hotel.entity.Room;
import com.tangtanwei.hotel.entity.User;
import com.tangtanwei.hotel.entity.bo.CommonConstant;
import com.tangtanwei.hotel.entity.enums.OrderFieldEnums;
import com.tangtanwei.hotel.entity.enums.OrderStatusEnums;
import com.tangtanwei.hotel.mapper.OrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    private final RoomService roomService;

    private final HttpSession session;

    public OrderService(RoomService roomService, HttpSession session) {
        this.roomService = roomService;
        this.session = session;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public String saveOrUpdateS(Order entity) {
        if (entity.getId() == null){
            // 新增订单，对应房间数量下降
            Room room = roomService.getById(entity.getRoomid());
            int res = room.getCount() - entity.getRoomCount();
            if (res < 0)
                return "房间已满";

            roomService.saveOrUpdate(Room.builder().id(entity.getRoomid()).count(res).build());

            User user = (User)session.getAttribute(CommonConstant.USER_KEY);
            if (user == null)
                return "用户未登陆,提交失败";
            entity.setUserId(user.getId());
            entity.setOrdertime(new SimpleDateFormat("yyyy-mm-dd").format(new Date()));
            entity.setStatus(OrderStatusEnums.CONFIRM.value);
        }else  {
            // 用户退房，订单完成,对应房间数量 增加
            if (OrderStatusEnums.FINISH.value.equals(entity.getStatus())){
                Room room = roomService.getById(entity.getRoomid());
                roomService.saveOrUpdate(Room.builder().id(entity.getRoomid())
                            .count(room.getCount()+entity.getRoomCount()).build());
            }
        }
         super.saveOrUpdate(entity);
        return "保存成功";
    }

    public List<Order> searchOrder(String keyword){
        return  this.list(new QueryWrapper<Order>().like("user_name",keyword)
                .or().like("idCard",keyword).or().like("phone",keyword)
                .or().like("checkInTime",keyword)
                .or().like("type",keyword).or().like("checkOutTime",keyword)
                .or().like("checkOutTime",keyword).or().like("price",keyword)
                .or().like("status",keyword));
    }


    public List<Order> getAllByConfitionField(int enums, String value){
        if (enums == OrderFieldEnums.Status.value){
            return this.list(new QueryWrapper<Order>().eq("status",value));
        }else if (enums == OrderFieldEnums.Type.value ){
            return this.list(new QueryWrapper<Order>().eq("type",value));
        }
        return null;
    }

    public String deleteOrderById(String id) {
        return this.removeById(id) ? "删除成功": "删除失败";
    }
}
